// pages/use-log/use-log.js
import {
  detail,
  approve,
  commentList
} from '../../api/apis.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    commentsList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      did: options.did
    })
    this._initData()
    this._getComments()
  },
  _initData() {
    detail({
      data: {
        id: this.data.did,
        openid: app.globalData.openid
      },
      success: res => {
        if (res.state) {

          let detail = res.data.content === null ? '' : res.data.content.replace(/style=\"/g, 'style="max-width:100%; ')
          detail = detail.replace(/<img/g, "<img style='max-width:100%'")
          detail = detail.replace(/<section/g, "<div")
          detail = detail.replace(/<\/section/g, "</div")
          res.data.content = detail
          this.setData({
            detail: res.data   //this.data.detail.replace(/<img/g, "<img style='max-width:100%'")
          })
        }
      }
    })
  },
  setApprove(e) {
    approve({
      data: {
        openid: app.globalData.openid,
        id: this.data.did
      },
      success: res => {
        if (res.state) {
          this._initData()
        }
      }
    })
  },
  _getComments() {
    commentList({
      data:{
        index: 1,
        size: 100,
        id: this.data.did
      },
      success: res => {
        if (res.state) {
          this.setData({
            commentsList: this.data.commentsList.concat(res.data.list)
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})